package com.pond.pond.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pond.common.core.context.SecurityContextHolder;
import com.pond.pond.domain.PondOrder;
import com.pond.pond.domain.PondOrderItem;
import com.pond.pond.mapper.OrderItemMapper;
import com.pond.pond.mapper.OrderMapper;
import com.pond.pond.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<PondOrderItem> selectOrderItemsListByOrderId(Long orderId) {
        QueryWrapper<PondOrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return orderItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<PondOrder> selectOrderListByUserId() {
        Long userId = SecurityContextHolder.getUserId();
        QueryWrapper<PondOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public Long insertOrder(PondOrder pondOrder) {
        if(orderMapper.insert(pondOrder) == 1){
            return pondOrder.getId();
        }else{
            return 0L;
        }
    }

    @Override
    public int deleteOrderByIds(Long[] ids) {
        return orderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int deleteOrderItemsByIds(Long[] ids) {
        return orderItemMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
